<!-- home -->
<template>
  <div>
    <van-nav-bar title="详情" left-arrow :fixed="true" :placeholder="true" @click-left="$router.go(-1)" />
    <van-swipe class="my-swipe" indicator-color="#000">
      <van-swipe-item v-if="data.avatar">
        <img :src="data.avatar" class="img">
      </van-swipe-item>
    </van-swipe>
    <div class="index-container" v-if="data">
      <div class="info-box">
        <div class="title">{{data.name}}</div>
        <div class="sub-title" v-if="isCourse">{{data.desc}}</div>
        <div class="time">
          <span v-if="data.user.name">{{data.user.name}}</span><span>{{data.updatedAt}}</span>
          <div class="edit-box">
            <van-icon :name="isLike ? 'like' : 'like-o'" :color="isLike ? '#ff9235' : '#999'" @click="handleLike">{{ countLike
            }}</van-icon>
            <van-icon :name="isSave ? 'star' : 'star-o'" :color="isSave ? '#ff9235' : '#999'" @click="handleSave">{{countSave
            }}</van-icon>
          </div>
        </div>
      </div>
      <div v-html="data.content"></div>
    </div>
  </div>
</template>

<script>
import { articleById,likeArticleById,saveArticleById,likeArticleAdd,likeArticleDel,saveArticleAdd,saveArticleDel } from '@/api/article.js'
export default {
  data() {
    return {
      isLike: false,
      isSave: false,
      data:{},
      countLike:0,
      countSave:0,
      id: '',
      userId:0,
      likeId:null,
      saveId:null,
      isCourse:false
    }
  },

  computed: {},

  mounted() {
    if (this.id) {
      this.articleById()
      this.likeArticleById()
      this.saveArticleById()
    }
  },
  created() {
    this.userId = this.$store.getters.userId
    this.id = Number(this.$route.query.id)
    this.isCourse = this.$route.query.course

  },
  methods: {
    async articleById() {
        await articleById({ id: this.id },this.isCourse).then(res => {
        if (res.code == 200) {
          this.data = res.data
        } else {
          this.$notify(res.message)
        }
      })
    },
    async likeArticleById() {
      await likeArticleById({ articleId: this.id },this.isCourse).then(res => {
        if (res.code == 200) {
          this.countLike = res.data.length
          res.data.forEach((item,index) => {
              if(item.userId == this.userId){
                  this.isLike = true
                  this.likeId = item.id
              }
          });

        } else {
          this.$notify(res.message)
        }
      })
    },
    async like() {
      await likeArticleAdd({
          articleId: this.id ,
          userId:this.userId},this.isCourse
        ).then(res => {
        if (res.code == 200) {
          this.likeArticleById()
        } else {
          this.$notify(res.message)
        }
      })
    },
    async unlike() {
      await likeArticleDel({
          id: this.likeId },this.isCourse
        ).then(res => {
        if (res.code == 200) {
          this.likeArticleById()
        } else {
          this.$notify(res.message)
        }
      })
    },
    async saveArticleById() {
      await saveArticleById({ articleId: this.id},this.isCourse).then(res => {
        if (res.code == 200) {
          this.countSave = res.data.length
           res.data.forEach((item,index) => {
              if(item.userId == this.userId){
                  this.isSave = true
                  this.saveId = item.id
              }
          });
        } else {
          this.$notify(res.message)
        }
      })
    },
    async saveArticleAdd() {
      await saveArticleAdd({
          articleId: this.id ,
          userId:this.userId},this.isCourse
        ).then(res => {
        if (res.code == 200) {
          this.saveArticleById()
        } else {
          this.$notify(res.message)
        }
      })
    },
    async saveArticleDel() {
      await saveArticleDel({
          id: this.saveId},this.isCourse
        ).then(res => {
        if (res.code == 200) {
          this.saveArticleById()
        } else {
          this.$notify(res.message)
        }
      })
    },
    handleSave() {
         if(this.isSave){
            this.saveArticleDel()
        }else{
            this.saveArticleAdd()
        }
        this.isSave = !this.isSave
    },
    handleLike() {
        if(this.isLike){
            this.unlike()
        }else{
            this.like()
        }

        this.isLike = !this.isLike
    }
  }
}
</script>
<style lang="scss" scoped>
  .my-swipe{
    background: #fff;
    .van-swipe-item {
      height: 5rem;
      text-align: center;
      .img{
        height: 100%;
        width: 100%;
      }
    }
  }
.index-container {
  padding: 0.4444rem;
  box-sizing: border-box;
  width: 100%;
  min-height: 100vh;
  background: #fff;
  .info-box {
    padding: 0 0 0.4rem 0;
    .title {
      font-size: 0.4444rem;
      line-height: 0.6rem;
      font-weight: 600;
    }
    .sub-title {
      font-size: 0.3889rem;
      color: #999;
      padding: 0.1389rem 0;
    }
    .time {
      color: #999;
      font-size: 0.3333rem;
      margin-top: 0.3rem;
      position: relative;
      width: 100%;
      span {
        margin-right: 0.4rem;
      }
      .edit-box {
        position: absolute;
        right: 0;
        top: 50%;
        transform: translateY(-50%);
        font-size: 0.3333rem;
        .van-icon {
          display: inline-block;
          margin-left: 0.2778rem;
          &::before {
            margin-right: 0.1389rem;
            font-size: 0.3333rem;
            font-weight: bold;
          }
        }
      }
    }
  }
}
</style>
